const Team = require('../model').Team
const ObjectId = require('mongoose').Types.ObjectId

exports.fetchTeamByCode = function(team_code) {
  return Team.findOne({ team_code })
}

exports.fetchTeamById = function(teamId) {
  return Team.findById(teamId)
}

exports.findOneAndUpdate = function(query, update) {
  return Team.findOneAndUpdate(query, update, { new: true })
}

exports.addMemberById = function(teamId, userId) {
  return Team.findOneAndUpdate({
    _id: teamId
  }, {
    $addToSet: {
      members: userId
    }
  }, { new: true })
}

exports.createTeam = function(name, years, classes, dormitories) {
  return Team.create({ name, years, classes, dormitories })
}

exports.allowJoin = async function(teamId) {
  try {
    const team = await Team.findOne({ _id: teamId })
    return team.allow_join
  } catch (err) {
    return err
  }
}

exports.getTagsById = async function(teamId) {
  try {
    const team = await Team.findOne({ _id: teamId }).populate('management_tags')
    return team.management_tags
  } catch (err) {
    throw err
  }
}

exports.addTagById = function(teamId, tagId) {
  return Team.findOneAndUpdate({
    _id: teamId
  }, {
    $push: {
      management_tags: tagId
    }
  }, { new: true })
}

exports.removeTagById = function(teamId, tagId) {
  return Team.findOneAndUpdate({
    _id: teamId
  }, {
    $pull: {
      management_tags: tagId
    }
  }, { new: true })
}

exports.getMembersInTeam = async function(teamId, skip, limit) {
  try {
    const filter = '-arancia -password -followers -contrast'
    const team = await Team.findOne({ _id: teamId }, { members: { $slice: [skip, limit] }}).populate({ path: 'members', select: filter })
    return team.members
    // const data = await Team.aggregate([
    //   { $match: { _id: ObjectId(teamId) }},
    //   { $project: {
    //     total: { $size: '$members' },
    //     members: { $slice: ['$members', skip, limit] }
    //   }},
    //   {
    //     $unwind: '$members'
    //   },
    //   {
    //     $lookup: {
    //       from: 'users',
    //       localField: 'members',
    //       foreignField: '_id',
    //       as: 'member_local'
    //     }
    //   }
    // ])
    // return data
  } catch (err) {
    throw err
  }
}

exports.getAllAnnTag = function(teamId, onlyTags) {
  if (onlyTags) {
    return Team.findOne({ _id: teamId }).populate({
      path: 'announcement_tags',
      match: { 'delete': false },
      select: '-delete -list'
    }).select('announcement_tags')
  } else {
    return Team.findOne({ _id: teamId }).populate({
      path: 'announcement_tags',
      match: { 'delete': false },
      select: '-delete',
      populate: {
        path: 'list',
        select: 'title'
      }
    }).select('announcement_tags')
  }
}

exports.addAnnTag = function(teamId, tagId) {
  return Team.findOneAndUpdate({
    _id: teamId
  }, {
    $addToSet: { announcement_tags: tagId }
  })
}

exports.removeAnnTag = function(teamId, tagId) {
  return Team.findOneAndUpdate({
    _id: teamId
  }, {
    $pull: { announcement_tags: tagId }
  })
}
